﻿
using System.Collections.Generic;
using UnityEngine;

namespace ABA
{
    public sealed class Network : UnitySingleton<Network>
    {
        private INetworkManager m_NetworkManager = null;
        // private EventComponent m_EventComponent = null;

        /// <summary>
        /// 获取网络频道数量。
        /// </summary>
        public int NetworkChannelCount
        {
            get
            {
                return m_NetworkManager.NetworkChannelCount;
            }
        }

        /// <summary>
        /// 游戏框架组件初始化。
        /// </summary>
        protected override void OnLoad()
        {
            base.OnLoad();

            m_NetworkManager = new NetworkManager();

            m_NetworkManager.NetworkConnected += OnNetworkConnected;
            m_NetworkManager.NetworkClosed += OnNetworkClosed;
            m_NetworkManager.NetworkMissHeartBeat += OnNetworkMissHeartBeat;
            m_NetworkManager.NetworkError += OnNetworkError;
            m_NetworkManager.NetworkCustomError += OnNetworkCustomError;
        }

        /// <summary>
        /// 检查是否存在网络频道。
        /// </summary>
        /// <param name="name">网络频道名称。</param>
        /// <returns>是否存在网络频道。</returns>
        public bool HasNetworkChannel(string name)
        {
            return m_NetworkManager.HasNetworkChannel(name);
        }

        /// <summary>
        /// 获取网络频道。
        /// </summary>
        /// <param name="name">网络频道名称。</param>
        /// <returns>要获取的网络频道。</returns>
        public INetworkChannel GetNetworkChannel(string name)
        {
            return m_NetworkManager.GetNetworkChannel(name);
        }

        /// <summary>
        /// 获取所有网络频道。
        /// </summary>
        /// <returns>所有网络频道。</returns>
        public INetworkChannel[] GetAllNetworkChannels()
        {
            return m_NetworkManager.GetAllNetworkChannels();
        }

        /// <summary>
        /// 获取所有网络频道。
        /// </summary>
        /// <param name="results">所有网络频道。</param>
        public void GetAllNetworkChannels(List<INetworkChannel> results)
        {
            m_NetworkManager.GetAllNetworkChannels(results);
        }

        /// <summary>
        /// 创建网络频道。
        /// </summary>
        /// <param name="name">网络频道名称。</param>
        /// <param name="serviceType">网络服务类型。</param>
        /// <param name="networkChannelHelper">网络频道辅助器。</param>
        /// <returns>要创建的网络频道。</returns>
        public INetworkChannel CreateNetworkChannel(string name, ServiceType serviceType, INetworkChannelHelper networkChannelHelper)
        {
            return m_NetworkManager.CreateNetworkChannel(name, serviceType, networkChannelHelper);
        }

        /// <summary>
        /// 销毁网络频道。
        /// </summary>
        /// <param name="name">网络频道名称。</param>
        /// <returns>是否销毁网络频道成功。</returns>
        public bool DestroyNetworkChannel(string name)
        {
            return m_NetworkManager.DestroyNetworkChannel(name);
        }

        private void OnNetworkConnected(object sender, NetworkConnectedEventArgs e)
        {
            Event.inst.Send<NetworkConnectedEventArgs>((int)E_NET_EVENT.Connected, e);
        }

        private void OnNetworkClosed(object sender, NetworkClosedEventArgs e)
        {
            Event.inst.Send<NetworkClosedEventArgs>((int)E_NET_EVENT.Closed, e);
        }

        private void OnNetworkMissHeartBeat(object sender, NetworkMissHeartBeatEventArgs e)
        {
            Event.inst.Send<NetworkMissHeartBeatEventArgs>((int)E_NET_EVENT.MissHeartBeat, e);
        }

        private void OnNetworkError(object sender, NetworkErrorEventArgs e)
        {
            Event.inst.Send<NetworkErrorEventArgs>((int)E_NET_EVENT.Error, e);
        }

        private void OnNetworkCustomError(object sender, NetworkCustomErrorEventArgs e)
        {
            Event.inst.Send<NetworkCustomErrorEventArgs>((int)E_NET_EVENT.CustomError, e);
        }

        /// <summary>
        /// Update is called every frame, if the MonoBehaviour is enabled.
        /// </summary>
        void Update()
        {
            m_NetworkManager.Update(Time.deltaTime, Time.unscaledDeltaTime);
        }

        public void SendHeart()
        {
            //m_NetworkManager.SendOnceHeart();
        }
    }
}
